﻿using System.Reflection;
using Autofac;
using Autofac.Extras.DynamicProxy;
using netCoreStudy.Aop;
using static netCoreStudy.MyCommonMethod;
using Module = Autofac.Module;

namespace netCoreStudy
{
	/// <summary>
	/// 自己定义 要批量 注入那些 services
	/// </summary>
	public class CustomAutofacModule : Module
	{
		protected override void Load(ContainerBuilder builder)
		{
			// 获取当前程序集
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			// 通过命名空间  后缀 来进行 注入
			// 注入 efCore 查询 service 
			builder.RegisterAssemblyTypes(executingAssembly)
				.Where(t => t.Namespace != null && (t.Namespace.EndsWith($"{GetAssemblyName()}.Service")
					))
				.AsImplementedInterfaces()
				.InstancePerLifetimeScope() // 生命周期 scope 
				.EnableInterfaceInterceptors() // 让他支持aop
				;

			// 注入 fluent Validation  验证 service 
			builder.RegisterAssemblyTypes(executingAssembly)
				.Where(t => t.Namespace != null && (t.Namespace.Contains($"{GetAssemblyName()}.DtoValidator")))
				.AsImplementedInterfaces()
				.InstancePerDependency() // 生命周期  transient
				;
			builder.RegisterType<CustomAutofacAop>(); // 注册下 aop 
		}
	}
}